package com.sinodata.ns.updatapw;

import java.sql.Connection;
import java.util.HashMap;
import java.util.Map;


import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import jt.classic.system.ISystem;
import jt.classic.system.database.SystemDataSource;
import jt.classic.system.log.LogManager;
import jt.classic.system.user.IUser;
import jt.classic.system.user.UserManager;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.DispatchAction;
import org.limp.basework.SimpleBean4Table;
import org.limp.basework.impl.CommonBean4HSQ;
import org.limp.basework.impl.SimpleBean4TableImpl;
import org.limp.basework.tools.BaseworkUtil;
import org.limp.mine.EncryptTool;
import org.limp.mine.StringTool;
import org.limp.mine.XMLTool;

public class Updatepw extends DispatchAction {
	
	public ActionForward updatepwd(ActionMapping mapping, ActionForm form,
	        HttpServletRequest request, HttpServletResponse response)
	        throws Exception {
			CommonBean4HSQ cbh = new CommonBean4HSQ(request);

	        Map params = cbh.getResource();

	        String opwd = (String) params.get("opwd");
	        String logid = (String) params.get("logid");
	        String pwd = (String) params.get("pwd"); 
	        Map resultMap = new HashMap();

	        resultMap.put("opera", "updatePw");
	        if (!logid.equals("") && !opwd.equals("") && !pwd.equals("")) {
	            IUser user = null;

	            Connection conn = null;

	            try {
	                SystemDataSource ds = SystemDataSource.getInstance();
	                conn = ds.getConnection();

	                UserManager manager = new UserManager(conn);

	                user = manager.logcheck(logid, pwd);
	            } catch (Exception ex) {
	            	resultMap.put("status","1");
	            	resultMap.put("re", "{success: false, errCode: '-2'}");
	                ISystem.catchException(request, this, ex);
	            } finally {
	                SystemDataSource.closeConnection(conn);
	            }

	            if (user != null) {
	                try {
	                    SystemDataSource ds = SystemDataSource.getInstance();
	                    conn = ds.getConnection();

	                    SimpleBean4Table sbt = new SimpleBean4TableImpl("ISYS_USER");

	                    sbt.getResource().put("uid", user.id());
	                    sbt.getResource().put("pwd", EncryptTool.encryptString(opwd));

	                    BaseworkUtil util = new BaseworkUtil();

	                    Map result = util.manualUpdate1(sbt, conn);

	                    LogManager lm = new LogManager(conn);

	                    lm.log(user, request.getRemoteAddr(), "", "",
	                        "updaterUserPWD", "update", result.toString());

	                    conn.commit();
	                    resultMap.put("status","0");
	                    resultMap.put("re", "");
	                } catch (Exception ex) {
	                	resultMap.put("status","1");
	                	resultMap.put("re",
	                        "{success: false, errCode: '-4'}");
	                    ISystem.catchException(request, this, ex);

	                    try {
	                        conn.rollback();
	                    } catch (Exception ex1) {
	                        ISystem.catchException(request, this, ex1);
	                    }
	                } finally {
	                    SystemDataSource.closeConnection(conn);
	                }
	            } else {
	            	resultMap.put("status","1");
	            	resultMap.put("re", "{success: false, errCode: '-3'}");
	            }
	        } else {
	        	resultMap.put("status","1");
	        	resultMap.put("re", "{success: false, errCode: '-1'}");
	        }
	        String result = XMLTool.result2XML(resultMap);
	        System.out.println(result);
	        request.setAttribute("result", result);
	        return mapping.findForward("result");

		}
}